package xjh.him.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.xjh.him.R;

import java.util.Arrays;

import xjh.him.adapter.MsgListAdapter;
import xjh.him.bean.MsgBean;
import xjh.him.common.SharedData;
import xjh.him.entity.Chat;
import xjh.him.entity.User;
import xjh.him.netty.codec.Command;
import xjh.him.netty.codec.Packet;
import xjh.him.netty.dto.ChatDto;
import xjh.him.util.GlideUtil;
import xjh.him.util.ToastUtil;

public class UserInfoActivity extends Activity {
    private ImageView infoBack, infoAvatar;
    private TextView name, nickname;
    private Button infoSendMsg, infoVideoCall;

    @SuppressLint("ResourceAsColor")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);

        init();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SharedData.REQ_CODE_CHAT &&
                resultCode == SharedData.RESULT_CODE_CHAT) {
            setResult(resultCode);
            finish();
        }
    }

    public void init() {
        User user = (User) getIntent().getSerializableExtra("chatter");
        infoAvatar = findViewById(R.id.info_avatar);
        name = findViewById(R.id.info_name);
        nickname = findViewById(R.id.info_nickname);

        GlideUtil.loadImage(this, user.getAvatarUrl(), infoAvatar,
                GlideUtil.defaultRoundedAvatarOpt());
        name.setText(user.getName());
        nickname.setText(user.getNickname());

        onBackClick();
        onSendMsgClick();
        onVideoCallClick();
    }

    public void onBackClick() {
        infoBack = findViewById(R.id.info_back);
        infoBack.setOnClickListener((view) -> {
            finish();
        });
    }

    public void onSendMsgClick() {
        infoSendMsg = findViewById(R.id.info_send_msg);
        infoSendMsg.setOnClickListener((view) -> {
            Intent intent = getIntent();
            User me = SharedData.me;
            User chatter = (User) intent.getSerializableExtra("chatter");
            MsgListAdapter msgListAdapter = SharedData.msgListAdapter;

            // 1. get chatId
            int chatId = 0;
            if (!msgListAdapter.getChatterSet().contains(chatter.getId())) {
                ChatDto.InsertReq req = new ChatDto.InsertReq();
                req.setMeId(me.getId());
                req.setType(SharedData.CHAT_TYPE_SINGLE);
                req.setMemberIds(Arrays.asList(chatter.getId()));
                SharedData.client.sendMsg(new Packet(
                        Command.getCommand(ChatDto.InsertReq.class),
                        JSON.toJSONString(req)
                ));
                Object obj = SharedData.future.get();
                if (obj instanceof ChatDto.InsertResp) {
                    ChatDto.InsertResp resp = (ChatDto.InsertResp) obj;
                    chatId = resp.getChatId();
                }
            }else {
                for (MsgBean bean : msgListAdapter.getBeans()) {
                    if (bean.getChat().getChatterId().intValue() == chatter.getId().intValue()) {
                        chatId = bean.getChat().getId();
                        break;
                    }
                }
            }

            // 2. msgList add item
            if (!msgListAdapter.getChatterSet().contains(chatter.getId())) {
                MsgBean bean = new MsgBean();
                Chat chat = new Chat();
                chat.setId(chatId);
                chat.setChatType(SharedData.CHAT_TYPE_SINGLE_STR);
                chat.setOwnerId(me.getId());
                chat.setChatterId(chatter.getId());
                chat.setName(chatter.getNickname());
                chat.setChatAvatar(chatter.getAvatarUrl());
                bean.setChat(chat);
                bean.setChatMsg(null);
                msgListAdapter.addLast(bean);
                msgListAdapter.getChatterSet().add(chatter.getId());
            }

            // 3. start chat page
            Intent chatIntent = new Intent(UserInfoActivity.this, ChatActivity.class);
            chatIntent.putExtra("chatter", chatter);
            chatIntent.putExtra("chat_id", chatId);
            startActivityForResult(chatIntent, SharedData.REQ_CODE_CHAT);
        });
    }

    public void onVideoCallClick() {
        infoVideoCall = findViewById(R.id.info_video_call);
        infoVideoCall.setOnClickListener((view) -> {
            ToastUtil.show(this, "得加钱");
        });
    }
}
